package com.sia.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import com.sia.springcloud.result.GridDataResult;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.spi.CalendarDataProvider;

/**
 * @Author: Sirius
 * @Date: 2022/5/17 16:08
 * @Description: 通过注册中心Eureka获取数采协议微服务信息
 **/
@Slf4j
@Service
public class ProtocolService {


    @Resource
    private DiscoveryClient discoveryClient;


    public Map<String, String> getProtocolMap() {
        Map<String, String> protocolMap = new HashMap<>();

        List<String> services = discoveryClient.getServices();

        for (String service : services) {
            String protocolName;

            String[] split = service.split("-");
            if (split.length >= 2 && split[0].equals("protocol")) {
                if (!split[1].equals("core")) {
                    protocolName = split[1];

                    List<ServiceInstance> instances = discoveryClient.getInstances(service);

                    if (instances.size() > 0) {
                        protocolMap.put(protocolName, instances.get(0).getInstanceId());
                    }

                }
            }


        }

        log.info(protocolMap.toString());
        return protocolMap;

    }


    /**
     * 获取正在运行的数采协议微服务的种类
     *
     * @return
     */
    public String getProtocolList() {
        log.info("获取数采协议微服务列表");
        Map<String, String> protocolMap = getProtocolMap();


        ArrayList<String> protocolList = new ArrayList<>();

        for (HashMap.Entry<String, String> entry : protocolMap.entrySet()) {
            protocolList.add(entry.getKey());
        }
        GridDataResult<String> gridDataResult = new GridDataResult<>();
        gridDataResult.setTotal(protocolList.size());
        gridDataResult.setRoot(protocolList);
        return JSON.toJSONString(gridDataResult);
    }


    /**
     * 根据协议名获取对应的微服务实例路径
     * 如果存在就返回
     * 没有就返回null
     *
     * @param request
     * @return
     */
    public String getInstanceIdByProtocolName(HttpServletRequest request) {
        String protocolName;
        try {
            protocolName = request.getParameter("protocolName");
        } catch (Exception e) {
            return null;
        }

        return getInstanceIdByProtocolName(protocolName);
    }

    /**
     * 根据协议名获取对应的微服务实例路径
     * 如果存在就返回
     * 没有就返回null
     *
     * @param protocolName
     * @return
     */
    public String getInstanceIdByProtocolName(String protocolName) {

        log.info("获取协议名为 " + protocolName + " 的微服务的名字。。。");

        Map<String, String> protocolMap = getProtocolMap();
        return protocolMap.get(protocolName);
    }


}
